﻿using System;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using CustomHelpers.Extensions;
using Infrasturcture.Global.Controls.LobbyMap;
using Infrasturcture.TouchLibrary;

namespace CustomControls.InheritedFrameworkControls
{
    public class MapElement : IMapElement
    {
        private byte _previousG;
        public string Id { get; set; }
        public IMTContainer Container { get; set; }
        public Viewbox Viewbox { get; set; }

        public int FloorId { get; set; }

        public event EventHandler Activated;

        private void Highlight()
        {
            var paths = Viewbox.FindChildren<Path>();
            foreach (var path in paths)
            {
                var brush = ((SolidColorBrush) path.Fill);
                if(brush==null) continue;
                var color = brush.Color;
                _previousG = color.G;
                path.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte) (255), color.B));
            }
            var rectangles = Viewbox.FindChildren<Rectangle>();
            foreach (var rectangle in rectangles)
            {
                var brush = ((SolidColorBrush)rectangle.Fill);
                if (brush == null) continue;
                var color = brush.Color;
                _previousG = color.G;
                rectangle.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte)(255), color.B));
            }
            var ellipses = Viewbox.FindChildren<Ellipse>();
            foreach (var ellipse in ellipses)
            {
                var brush = ((SolidColorBrush)ellipse.Fill);
                if (brush == null) continue;
                var color = brush.Color;
                _previousG = color.G;
                ellipse.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte)(255), color.B));
            }
        }

        private void UnHighlight()
        {
            var paths = Viewbox.FindChildren<Path>();
            foreach (var path in paths)
            {
                var brush = ((SolidColorBrush)path.Fill);
                if (brush == null) continue;
                var color = brush.Color;
                path.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte)(_previousG), color.B));
            }
            var rectangles = Viewbox.FindChildren<Rectangle>();
            foreach (var rectangle in rectangles)
            {
                var brush = ((SolidColorBrush)rectangle.Fill);
                if (brush == null) continue;
                var color = brush.Color;
                rectangle.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte)(_previousG), color.B));
            }
            var ellipses = Viewbox.FindChildren<Ellipse>();
            foreach (var ellipse in ellipses)
            {
                var brush = ((SolidColorBrush)ellipse.Fill);
                if (brush == null) continue;
                var color = brush.Color;
                ellipse.Fill = new SolidColorBrush(Color.FromArgb(color.A, color.R, (byte)(_previousG), color.B));
            }
        }

        public void Activate()
        {
            Highlight();
            if(Activated!=null)
                Activated(this,new EventArgs());
        }

        public void DeActivate()
        {
            UnHighlight();
        }
    }
}
